package com.cwl.httpclient;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientTest {
    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet("http://localhost:8888");
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity);
        long length = entity.getContentLength();
        for (Header allHeader : response.getAllHeaders()) {
            System.out.println("响应头为： " + allHeader.getName() + ":" + allHeader.getValue());

        }
        System.out.println("内容状态为：" + response.getStatusLine());
        System.out.println("内容长度：" + length);
        System.out.println("内容为：" + content);

    }
}
